<?php


namespace app\admin\services\task;


use app\admin\model\CheckWorkFace;
use app\admin\model\Distributor;
use app\admin\model\EquIp;
use app\admin\model\FaceRecognitionStaff;
use app\admin\model\ShopClerks;

class CheckWorkService
{

    public static function initialization($data)
    {
        //查出当前所有店铺的考勤信息
        $shops = Distributor::field('a.id as shop_id,a.check_work_id,b.*')
            ->alias('a')
            ->leftJoin('lg_check_work_attendance b','a.check_work_id = b.id')
            ->where('a.status',1)
            ->where('b.status',2)
            ->whereNotNull('check_work_id')
            ->select();
        foreach ($shops as $key => $value){
            //验证设备是否正常运行
            $check_equip = EquIp::check_shop_equip($value['shop_id']) ;
            if(!$check_equip){
                continue;
            }
            $check = self::check_week($value['check_week_begin'],$value['check_week_over']);
            if(!$check){
                continue;
            }
            //判断上下班时间
            $check_go = self::check_work_time($value['go_work_time'],$value['off_work_time'],$value);
            if(!$check_go){
                continue;
            }
        }
        return true;
    }

    //验证是否在考情中
    public static function check_week($begin,$over)
    {
        $week = date('w');
        $check_array = self::check_number($begin,$over);
        if(!in_array($week,$check_array)){
            return false;
        }
        return true;
    }

    //拿出所要的星期考情数
    public static function check_number($begin,$over)
    {
        if($over < $begin){
            $begins = $begin ;
            $begin = $over ;
            $over = $begins;
        }
        $array = [0,1,2,3,4,5,6] ;
        $new_array = [] ;
        foreach ($array as $key => $value){
            if($value >= $begin && $value <= $over){
                $new_array[$key] = $value;
            }
        }
        return $new_array;
    }

    //上班时间判断
    public static function check_work_time($begin_time,$over_time,$value)
    {
        //获取开始考勤时间
        $go_time = strtotime($begin_time) - 1800 ;
        if(time() < strtotime($begin_time) && time() > $go_time ){
            //验证是否已经打上班卡了
            $check_go = self::check_go_punch_date(1,$value['shop_id']);
            if(!$check_go){
                return false;
            }
            //上班时间考勤
            self::check_go_job($value);
        }else if (time() > strtotime($over_time)){
            //验证是否已经打下班卡了
            $check_go = self::check_go_punch_date(2,$value['shop_id']);
            if(!$check_go){
                return false;
            }
            self::check_off_work($value);

        }else{
            return false;
        }
    }

    //验证今天是否已经打卡
    public static function check_go_punch_date($type,$shop_id)
    {
        $date = date('Y-m-d');
        $where = array(
            'shop_id' => $shop_id ,
            'punch_date' => $date ,
            'type' => $type
        );
        $check = CheckWorkFace::where($where)->find();
        if($check){
            return false;
        }
        return true;
    }

    //上班考勤队列
    public static function check_go_job($value)
    {
        $go_time = strtotime($value['go_work_time']) - 1800 ;
        $end_time = strtotime($value['go_work_time']) ;
        $clerks = ShopClerks::field('face_id')->where('shop_id','like','%'.$value['shop_id'].'%')->select();
        foreach ($clerks as $key => $values){
            $face_ids[$key] = $values['face_id'];
        }
        $face = FaceRecognitionStaff::whereIn('face_id',$face_ids)->whereBetween('enter_time', $go_time.','.$end_time)->order('id asc')->find();
        if (!$face){
            return false;
        }
        $add_data = array(
            'face_id' => $face['face_id'] ,
            'clerks_id' => $face['shop_clerks_id'] ,
            'shop_id' => $value['shop_id'] ,
            'check_work_id' => $value['check_work_id'] ,
            'check_time' => time() ,
            'type' => 1 ,
            'punch_time' => $face['enter_time'] ,
            'staff_id' => $face['id'] ,
            'punch_date' => date('Y-m-d',time()) ,
            'create_time' => time() ,
            'update_time' => time() ,
        );
        $result = CheckWorkFace::create($add_data);
        return true;
    }

    //下班考勤队列
    public static function check_off_work($value)
    {
        $end_time = strtotime($value['off_work_time']) ;
        $clerks = ShopClerks::field('face_id')->where('shop_id','like','%'.$value['shop_id'].'%')->select();
        foreach ($clerks as $key => $values){
            $face_ids[$key] = $values['face_id'];
        }
        $face = FaceRecognitionStaff::whereIn('face_id',$face_ids)->where('enter_time', '>',$end_time)->order('id asc')->find();
        if (!$face){
            return false;
        }
        $add_data = array(
            'face_id' => $face['face_id'] ,
            'clerks_id' => $face['shop_clerks_id'] ,
            'shop_id' => $value['shop_id'] ,
            'check_work_id' => $value['check_work_id'] ,
            'check_time' => time() ,
            'type' => 2 ,
            'punch_time' => $face['enter_time'] ,
            'staff_id' => $face['id'] ,
            'punch_date' => date('Y-m-d',time()) ,
            'create_time' => time() ,
            'update_time' => time() ,
        );
        $result = CheckWorkFace::create($add_data);
        return true;
    }

}